<template>
  <view>
    <s-layout
      title="首页"
      navbar="111"
      tabbar="/pages/index/index"
      :navbarStyle="template.style?.navbar"
      onShareAppMessage
      :bgStyle="{ backgroundColor: '#fff' }"
    >
      <!-- 	<view class="flex justify-between" style="margin-top: 120rpx;padding: 20rpx;text-align: center;background-color: #e32a22;">
				<view v-for="(item,idx) in headMenuList" :key="idx">
					<view style="width: 90rpx; height: 90rpx; background-color: #fff;border-radius: 10rpx;margin: 0 auto;">
						<image :class="item.class" style="width: 100rpx;height: 100rpx;" :src="item.icon"></image>
					</view>
					<view style="font-size: 34rpx;margin-top: 20rpx;color: #fff;font-weight: bold;">{{ item.name }}</view>
				</view>
			</view> -->

      <!-- <view>
			      <u-notice-bar :text="text1"></u-notice-bar>
			    </view> -->

      <view class="head-box">
        <view style="margin-left: 20rpx; margin-top: 30rpx" class="flex">
          <view style="width: 400rpx">
            <s-search-block
              :placeholder="'搜索关键字'"
              :radius="20"
              width="250px"
              elBackground="#fff"
              @click="sheep.$router.go('/pages/index/search')"
            ></s-search-block>
          </view>
          <!-- <view style="margin-left: 100rpx; margin-top: 10rpx">
            <image
              style="width: 50rpx; height: 50rpx"
              src="https://ihuanggang-live-1323882773.cos.ap-nanjing.myqcloud.com//image/2025/01/03/9ea4da0f0bb242d8863804151ee70c42.png"
            >
            </image>
          </view> -->
        </view>
      </view>
      <!--轮播图 -->
      <view class="banner-content-box">
        <view class="bgc"></view>
        <view class="banner-content">
          <swiper
            class="swiper-content"
            style="height: 300rpx"
            :indicator-dots="true"
            :autoplay="true"
          >
            <swiper-item
              v-for="(it, idx) in bannerList"
              style="height: 300rpx"
              :key="it.id"
              @tap="clickBanner(it)"
            >
              <image
                style="width: 710rpx; height: 300rpx; border-radius: 20rpx"
                :src="it.picture"
                class="img"
              />
            </swiper-item>
          </swiper>
        </view>
      </view>

      <!-- 分类 -->
      <view class="">
        <!-- <view class="category-content">
					<view class="category-item max-item" v-for="(it,idx) in categoryGoMenu"
						@click="goTobigCatrgory(it)" :key="idx">
						<image :src="it.icon" class="ct-icon" style="border-radius: 50%;" />
						<view class="ct-text">{{it.name}}</view>
					</view>
				</view> -->

        <view style="margin-bottom: 20rpx">
          <!-- <swiper
            ref="aa"
            style="height: 700rpx"
            :indicator-dots="true"
            :autoplay="false"
          >
            <swiper-item>
              <view class="category-content max-item">
                <view
                  class="category-item"
                  v-for="(it, idx) in categoryList.slice(0, 10)"
                  @tap="toGoodsInfo(it)"
                  :key="idx"
                >
                  <image :src="it.icon" class="ct-icon" />
                  <view class="ct-text">{{ it.name }}</view>
                </view>
              </view>
            </swiper-item>

            <swiper-item
              v-if="categoryList.length > 8"
              style="background-color: #fff"
            >
              <view class="category-content max-item">
                <view
                  class="category-item"
                  v-for="(it, idx) in categoryList.slice(10, 20)"
                  @tap="
                    sheep.$router.go('/pages/goods/list', { categoryId: it.id })
                  "
                  :key="idx"
                >
                  <image :src="it.icon" class="ct-icon" />
                  <view class="ct-text">{{ it.name }}</view>
                </view>
              </view>
            </swiper-item>

            <swiper-item
              v-if="categoryList.length > 8"
              style="background-color: #fff"
            >
              <view class="category-content max-item">
                <view
                  class="category-item"
                  v-for="(it, idx) in categoryList.slice(20, 30)"
                  @tap="
                    sheep.$router.go('/pages/goods/list', { categoryId: it.id })
                  "
                  :key="idx"
                >
                  <image :src="it.icon" class="ct-icon" />
                  <view class="ct-text">{{ it.name }}</view>
                </view>
              </view>
            </swiper-item>
          </swiper> -->

          <swiper
            style="height: 310rpx"
            :indicator-dots="true"
            :autoplay="false"
          >
            <swiper-item
              v-for="(page, pageIndex) in paginatedCategories"
              :key="pageIndex"
            >
              <view class="category-content max-item">
                <view
                  class="category-item"
                  v-for="(it, idx) in page"
                  :key="idx"
                  @tap="toGoodsInfo(it)"
                >
                  <image :src="it.icon" class="ct-icon" />
                  <view class="ct-text">{{ it.name }}</view>
                </view>
              </view>
            </swiper-item>
          </swiper>
        </view>
      </view>

      <!--ad -->
      <view style="margin-bottom: 20rpx">
        <swiper
          style="height: 200rpx; width: 710rpx; margin: 0 auto"
          :indicator-dots="false"
          :autoplay="true"
        >
          <swiper-item
            v-for="(it, indexd) in ADList"
            style="height: 300rpx"
            :key="it.id"
            @tap="clickBanner(it)"
          >
            <image
              style="width: 710rpx; height: 200rpx; border-radius: 20rpx"
              :src="it.picture"
              class="img"
            />
          </swiper-item>
        </swiper>
      </view>

      <!-- 广告模块 -->
      <!--      <s-popup-image />-->
      <!--      <view class="icon-text">-->
      <!--        <view class="text-info">-->
      <!--          <view class="left"></view>-->
      <!--          <view class="center">-->
      <!--            推荐商品-->
      <!--          </view>-->
      <!--          <view class="right"></view>-->
      <!--        </view>-->
      <!--      </view>-->
      <view class="goods-block">
        <!-- goodsType="自营商品" -->
        <s-goods-card
          :key="refreshKey"
          :data="goodsCard.data"
          :styles="goodsCard.style"
        />
      </view>
    </s-layout>
  </view>
</template>

<script setup>
import { computed, nextTick, ref } from "vue";
import {
  onLoad,
  onPageScroll,
  onPullDownRefresh,
  onReachBottom,
  onShareAppMessage,
  onShow,
} from "@dcloudio/uni-app";
import sheep from "@/sheep";
import $share from "@/sheep/platform/share";
//#ifdef H5
import weixin from "@/sheep/libs/sdk-h5-weixin";
//#endif
import { userihuanggangLogin } from "@/sheep/hooks/useModal";

const pageSize = 10;
const paginatedCategories = computed(() => {
  const pages = [];
  for (let i = 0; i < categoryList.value.length; i += pageSize) {
    pages.push(categoryList.value.slice(i, i + pageSize));
  }
  return pages;
});

const text1 = ref(
  "uView UI众多组件覆盖开发过程的各个需求，组件功能丰富，多端兼容。让您快速集成，开箱即用"
);
const bannerList = ref([]);
const ADList = ref([]);
const categoryList = ref([]);
const categoryGoMenu = ref([]);
const headMenuList = ref([
  {
    name: "商户电子券",
    icon: "https://ihuanggang-live-1323882773.cos.ap-nanjing.myqcloud.com//image/2025/01/03/17fae9469e524bbc82d023d0616c974a.png",
    url: "",
    class: "dianziquan",
  },
  {
    name: "工会福利",
    icon: "https://ihuanggang-live-1323882773.cos.ap-nanjing.myqcloud.com//image/2025/01/03/3d2ef1424f2644329c35836194b42ab9.png",
    url: "",
    class: "gonghui",
  },
  {
    name: "优惠商户",
    icon: "https://ihuanggang-live-1323882773.cos.ap-nanjing.myqcloud.com//image/2025/01/03/28a2edb9cd604171a48634f174816021.png",
    url: "",
    class: "shanghu",
  },
  {
    name: "积分充值",
    icon: "https://ihuanggang-live-1323882773.cos.ap-nanjing.myqcloud.com//image/2025/01/03/f5444ecff857420b89890fbbc470c84c.png",
    url: "",
    class: "jifen",
  },
]);
const goodsCard = {
  data: {
    mode: 2,
    goodsFields: {
      title: {
        show: 1,
      },
      subtitle: {
        show: 1,
      },
      price: {
        show: 1,
      },
      original_price: {
        show: 1,
      },
      sales: {
        show: 1,
      },
      stock: {
        show: 0,
      },
    },
    buyNowStyle: {
      // "mode": 2,
      // "text": "立即购买",
      // "color1": "#E9B461",
      // "color2": "#EECC89",
      // "src": "\/storage\/decorate\/20221115\/4782356b4587dc4f4a218f2540a0bafc.png",
      // "right": '20rpx',
      // "bottom":"18rpx"
    },
    tagStyle: {
      show: 0,
      src: "",
    },
    params: {
      orderField: "sort",
      orderSort: "asc",
    },
    borderRadiusTop: 6,
    borderRadiusBottom: 6,
    space: 8,
  },
  style: {
    background: {
      type: "color",
      bgImage: "",
      bgColor: "",
    },
    marginLeft: 8,
    marginRight: 8,
    marginTop: 0,
    marginBottom: 10,
    borderRadiusTop: 0,
    borderRadiusBottom: 0,
    padding: 0,
  },
};

// 隐藏原生tabBar
uni.hideTabBar();

const template = computed(() => sheep.$store("app").template.home);
const barHeight = ref(0);

onLoad(async (options) => {
  const statusBarHeight = sheep.$platform.device.statusBarHeight;
  barHeight.value = statusBarHeight + 54 + "px";
  init(options);
});

function goTobigCatrgory(item) {
  // sheep.$router.go(item.url)

  if (["苏宁", "京东"].indexOf(item.name) > -1) {
    uni.showToast({
      icon: "none",
      title: "敬请期待",
    });
  } else {
    sheep.$router.go(item.url);
  }
}

function goToCategory(item) {
  sheep.$router.go(`/pages/index/category1?params=${JSON.stringify(item)}`);
}

async function getCategoryList() {
  const res = await sheep.$api.category.list();

  categoryList.value = [];
  categoryList.value = res.filter((item) => item.children?.length > 0);
}

function init(options) {
  sheep.$store("app").init();
  setTimeout(function () {
    uni.stopPullDownRefresh();
  }, 800);
  // 开始登录
  // getCategoryList()
  categoryMoudle();
  getCategoryList();
  carouselImageH5List();
  ADImageH5List();
  // userLogin()
  // userihuanggangLogin()

  // 预览模板
  if (options.templateId) {
    sheep.$store("app").init(options.templateId);
  }

  // 解析分享信息
  if (options.spm) {
    $share.decryptSpm(options.spm);
  }

  // 进入指定页面(完整页面路径)
  if (options.page) {
    sheep.$router.go(decodeURIComponent(options.page));
  }

  //#ifdef H5
  setOpenShare();
  //#endif
}
onShareAppMessage((res) => {
  let shareData = {
    title: "邀请好友领取海量现金券!",
    desc: "我正在使用xxxApp，赶紧跟我一起来体验！",
    link: "https://mall.ichengle.top/uni/",
    imgUrl:
      "https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/d8590190-4f28-11eb-b680-7980c8a877b8.png",
  };
  return {
    ...shareData,
  };
});

function toGoodsInfo(it) {
  console.log("toGoodsInfo", it);
  if (it.name === "电影") {
    sheep.$router.go("/pages/movie/index");
  } else if (it.name === "蛋糕") {
    sheep.$router.go("/pages/cake/index");
  } else {
    sheep.$router.go("/pages/goods/list", { categoryId: it.id });
  }
}

// 添加一个刷新标识
const refreshKey = ref(0);

// 下拉刷新
onPullDownRefresh(() => {
  sheep.$store("app").init();

  // 重新获取数据
  getCategoryList();
  carouselImageH5List();
  ADImageH5List();
  categoryMoudle();

  // 触发商品列表刷新
  refreshKey.value++;

  setTimeout(function () {
    uni.stopPullDownRefresh();
  }, 800);
});
// 上拉加载更多
onReachBottom(() => {
  // 该事件必须申明,子组件才会触发该事件
});
onPageScroll(() => {});
//#ifdef H5
// 微信h5分享；
function setOpenShare() {
  let currentUrl = location.href; //获取当前页面链接

  let shareData = {
    title: "邀请好友领取海量现金券!",
    desc: "我正在使用xxxApp，赶紧跟我一起来体验！",
    link: "https://mall.ichengle.top/uni/",
    imgUrl:
      "https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/d8590190-4f28-11eb-b680-7980c8a877b8.png",
  };
  console.log("更新分享", shareData);
  weixin.updateShareInfo(shareData);
}
//#endif
// 获取商品分类
// function getCategoryList() {
// 	sheep.$api.category.list().then(res => {
// 		// categoryList.value = res
// 	})
// }

// 获取轮播图列表
function carouselImageH5List() {
  sheep.$api.pageIndex.carouselImageH5List().then((res) => {
    bannerList.value = res.content;
    // console.log('carouselImageH5List',res)
  });
}

function ADImageH5List() {
  sheep.$api.pageIndex.ADImageH5List().then((res) => {
    ADList.value = res.content;
    // console.log('ADImageH5List',res)
  });
}

function categoryMoudle() {
  sheep.$api.pageIndex.categoryMoudle().then((res) => {
    categoryGoMenu.value = res;
    // console.log('categoryMoudle',res)
  });
}
</script>

<style lang="scss" scoped>
.head-box {
  display: flex;
  background-color: #e32a22;
  padding-top: 80rpx;
  height: 120rpx;
  width: 100vw;
  position: fixed;
  top: 0;
  z-index: +18;
}

.banner-content-box {
  position: relative;
  height: 420rpx;
  margin-top: 100rpx;
  background-color: #fff;

  .bgc {
    height: 350rpx;
    background-color: #e32a22;
    border-bottom-left-radius: 100rpx;
    border-bottom-right-radius: 100rpx;
  }

  .banner-content {
    position: absolute;
    left: 20rpx;
    top: 50px;
    height: 300rpx;
    margin-top: 300rpx;
    width: 710rpx;
    margin: 0 auto;
    // margin-top: 150rpx;
    z-index: +15;
  }
}

.see_ads {
  margin: 400rpx 40rpx 60rpx 40rpx;
  text-align: center;
  color: red;
}

.dianziquan,
.gonghui {
  width: 80rpx !important;
  height: 80rpx !important;
  margin-top: 5rpx;
}

.shanghu,
.jifen {
  width: 60rpx !important;
  height: 60rpx !important;
  margin-top: 14rpx;
}

.btn-content {
  display: flex;
  justify-content: center;
}

.see-btn {
  width: 190rpx;
  height: 70rpx;
  font-size: 28rpx;
  border: 2rpx solid #dfdfdf;
  border-radius: 35rpx;
  font-weight: 400;
  color: #595959;
}

.goods-block {
  /* #ifdef MP-WEIXIN */
  // margin: v-bind(barHeight) 20rpx 10rpx 20rpx;
  margin: 0 20rpx 10rpx 20rpx;
  /* #endif */

  /* #ifdef H5 */
  margin: 90rpx 20rpx 10rpx 20rpx;
  /* #endif */
}

.icon-text {
  display: flex;
  justify-content: center;
  margin-bottom: 24rpx;

  .text-info {
    display: flex;
    align-items: center;
    width: 440rpx;

    .left {
      width: 108rpx;
      height: 2rpx;
      background: #d8d8d8;
    }

    .center {
      width: 128rpx;
      height: 44rpx;
      font-size: 32rpx;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #333333;
      line-height: 44rpx;
      margin-left: 48rpx;
      margin-right: 48rpx;
    }

    .right {
      width: 108rpx;
      height: 2rpx;
      background: #d8d8d8;
    }
  }
}

.category-content {
  // margin-left: 40rpx;
  // margin-right: 40rpx;
  // margin-top: 100rpx;
  padding: 20rpx;
  // display: grid;
  display: flex;
  flex-wrap: wrap;
  // grid-template-columns: auto auto auto 112rpx;
  background-color: #fff;
  width: 710rpx;

  //grid-column-gap: v-bind(ml);

  .category-item {
    flex: 0 0 20%;
    margin-bottom: 20rpx;
    text-align: center;
    .ct-icon {
      width: 60rpx;
      height: 60rpx;
      border-radius: 50%;
    }

    .ct-text {
      text-align: center;
      margin-top: 10rpx;
      font-size: 28rpx;
      font-weight: bold;
    }
  }

  .max-item {
    flex: 0 0 20%;
    margin-bottom: 0;
  }
}
</style>
